package com.liziba.rpc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @auther LiZiBa
 * @date 2020/10/19 22:16
 * @description:
 **/
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;
    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 执行动态代理 远程调用
        // 1、组装参数
        // 2、序列化
        // 3、网络传输
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setClazzName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParams(args);
        rpcRequest.setTypes(method.getParameterTypes());

        //
        RpcNetTransport rpcNetTransport = new RpcNetTransport(host, port);
        return rpcNetTransport.send(rpcRequest);
    }
}
